<?php
/**
 * Created by PhpStorm.
 * User: 李闯
 * Date: 2020/7/21
 * Time: 13:28
 * Content:满减
 */

namespace app\admin\controller;

namespace app\admin\controller;
use app\reuse\controller\ResponseJson;
use app\service\ResourceService;
use think\Db;
use think\facade\Session;
class Reduction extends Common
{
    use ResponseJson;

    function __construct()
    {
        parent::__construct();
    }
    /*
     * @param null
     * @content 满减列表
     * @return array
     * */
    function index()
    {
        if(input("get.active") == "ajax"){
            $time=date("Y-m-d H:i:s");

            # 定义listRows
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 10;
            }
            $info = Db::table('reduction')
                ->order("id desc")
                ->paginate($listRows, false, ['query' => request()->param()]);
            $notice = $info->toArray();
            foreach($notice['data'] as $k=>$v){
                #当前时间>发送时间  并<下架时间
                if($v['start_time']<$time and  $time<=$v['end_time']){
                    #状态改为 已发送
                    $notice['data'][$k]['status']=1;

                }else if($v['start_time']>$time and  $time<$v['end_time'] ){
                    #当前时间<发送时间  并<下架时间 状态 改为 待发送  待显示 发送时间<=结束时间
                    $notice['data'][$k]['status']=2;

                }else{
                    #已结束
                    $notice['data'][$k]['status']=3;

                }

            }
            return $this->jsonSuccessData($notice);
        }else{
            return $this->fetch();

        }

    }
    /*
     * @param null
     * @content 满减修改
     * @return array
     * */
    function write(){
        #满减id
        $id= input("get.id");

        if(input("get.active")=="ajax"){
            $info = input("post.");

            Db::table("reduction")->where("id = $id")->update($info);
            return array("status"=>true,'msg'=>'操作成功');

        }else{
            #查询通知消息
            $reduction = Db::table("reduction")->where("id = $id")->find();
            $this->assign("reduction",$reduction);
            $this->assign("id",$id);
            return $this->fetch();
        }

    }
    /*
    * @param null
    * @content 满减创建
    * @return array
    * */
    function create(){
        if(input("get.active") == "ajax"){
            $time = date("Y-m-d H:i:s");

            $_where="'$time' > start_time and '$time'<= end_time";
            #查询通知消息状态 已展示
            $status = Db::table("reduction")->field("id")->where($_where)->find();
            if(!empty($status['id'])){
                return array("status"=>false,'msg'=>'有开始的活动不能创建新的哦！');

            }else{
                $info = input("post.");

                $notice_id = Db::table('reduction')->insertGetId($info);
                if($notice_id){
                    return array("status"=>true,'msg'=>'操作成功');
                }else{
                    return array("status"=>false,'msg'=>'提交失败');
                }
            }
        }else{
            return $this->fetch();
        }

    }
    /*
     * @param null
     * @content 删除
     * @return array
     * */

    function del(){
        $id =input("post.id");
        if(!empty($id)){
            #状态修改为删除
            Db::table("reduction")->where("id = $id")->delete();
            return array('status'=>true);
        }
    }
}